﻿using System;
using System.Collections.Generic;
using System.Linq;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Architecture;
using Autodesk.Revit.DB.Mechanical;
using YArchitech.Revit.Electric.Common;

namespace YArchitech.Revit.MEP.Calculator
{
	public class Common
	{
		public static FilteredElementCollector GetElementsOfType(Document doc, Type type, BuiltInCategory bic)
		{
			FilteredElementCollector filteredElementCollector = new FilteredElementCollector(doc);
			filteredElementCollector.OfCategory(bic);
			filteredElementCollector.OfClass(type);
			return filteredElementCollector;
		}

		public static List<Element> GetElementByCategory(Document doc, BuiltInCategory builtInCategory)
		{
			return new FilteredElementCollector(doc).OfCategory(builtInCategory).ToElements().ToList<Element>();
		}

		public static Space CreatSpace(Document doc, Room room)
		{
			Space result = null;
			LocationPoint locationPoint = room.Location as LocationPoint;
			Level level = doc.GetElementById(room.LevelId) as Level;
			if (level != null && locationPoint != null)
			{
				Space space = doc.Create.NewSpace(level, new UV(locationPoint.Point.X, locationPoint.Point.Y));
				space.Name = room.Name.Split(new char[]
				{
					' '
				})[0];
				Parameter parameter = space.GetParameter("房间ID");
				if (parameter == null)
				{
					Dictionary<string, ParameterType> dictionary = new Dictionary<string, ParameterType>();
                    dictionary.Add("房间ID", (ParameterType)1);
					foreach (KeyValuePair<string, ParameterType> keyValuePair in dictionary)
					{
						if (space.GetParameter(keyValuePair.Key) == null)
						{
							SharedParameter.InsertSharedParameter(doc, BuiltInCategory.OST_MEPSpaces, "电气数据", dictionary, true);
							break;
						}
					}
					parameter = space.GetParameter("房间ID");
				}
				parameter.Set(room.Id.ToString());
				result = space;
			}
			return result;
		}

		public static Space CreatSpace(Document documentLin, Document doc, Room room, Transform tsForm)
		{
			Space result = null;
			LocationPoint locationPoint = room.Location as LocationPoint;
			UV uv = new UV();
			XYZ xyz = locationPoint.Point + tsForm.Origin;
			uv = new UV(xyz.X, xyz.Y);
			Level level = documentLin.GetElementById(room.LevelId) as Level;
			Level level2 = null;
			foreach (Element element in RevitFilter.GetElementsOfType(doc, typeof(Level), BuiltInCategory.OST_Levels).ToElements().ToList<Element>())
			{
				if (Math.Abs((element as Level).Elevation - level.Elevation) < 0.1)
				{
					level2 = (element as Level);
					break;
				}
			}
			if (level2 == null)
			{
				throw new Exception("当前项目中未创建链接房间所需要的标高");
			}
			if (locationPoint != null)
			{
				Space space = doc.Create.NewSpace(level2, uv);
				space.Name = room.Name.Split(new char[]
				{
					' '
				})[0];
				Parameter parameter = space.GetParameter("房间ID");
				if (parameter == null)
				{
					Dictionary<string, ParameterType> dictionary = new Dictionary<string, ParameterType>();
                    dictionary.Add("房间ID", (ParameterType)1);
					foreach (KeyValuePair<string, ParameterType> keyValuePair in dictionary)
					{
						if (space.GetParameter(keyValuePair.Key) == null)
						{
							SharedParameter.InsertSharedParameter(doc, BuiltInCategory.OST_MEPSpaces, "电气数据", dictionary, true);
							break;
						}
					}
					parameter = space.GetParameter("房间ID");
				}
				parameter.Set(room.Id.ToString());
				result = space;
			}
			return result;
		}

		public static AnnotationSymbol IsExsitTag(Room room, IList<AnnotationSymbol> allRoomTag)
		{
			foreach (AnnotationSymbol annotationSymbol in allRoomTag)
			{
				try
				{
					if (annotationSymbol.GetParameter("房间ID").AsString() == room.Id.ToString())
					{
						return annotationSymbol;
					}
				}
				catch (Exception)
				{
				}
			}
			return null;
		}

		public static Space IsExsitSpace(Room room, IList<Space> listSpace)
		{
			foreach (Space space in listSpace)
			{
				if (space.GetBoundarySegments(new SpatialElementBoundaryOptions()).Count > 0)
				{
					try
					{
						if (space.GetParameter("房间ID").AsString() == room.Id.ToString())
						{
							return space;
						}
					}
					catch (Exception)
					{
					}
				}
			}
			return null;
		}

		public static List<string> GetLinkFilePaths(Document doc)
		{
			List<string> list = new List<string>();
			foreach (ElementId elementId in ExternalFileUtils.GetAllExternalFileReferences(doc))
			{
				if (doc.GetElement(elementId).IsExternalFileReference())
				{
					ExternalFileReference externalFileReference = doc.GetElement(elementId).GetExternalFileReference();
					if (1 == (int)externalFileReference.ExternalFileReferenceType)
					{
						list.Add(ModelPathUtils.ConvertModelPathToUserVisiblePath(externalFileReference.GetAbsolutePath()));
					}
				}
			}
			return list;
		}
	}
}
